package com.maozzi.passport.web.controller.rest;

import com.maozzi.api.user.UserService;
import com.maozzi.api.user.model.TokenDTO;
import com.maozzi.common.util.CloneUtil;
import com.maozzi.common.util.ParamCheckUtil;
import com.maozzi.passport.web.model.TokenVO;
import com.maozzi.passport.web.model.UserVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 登录 controller
 *
 * @author maozi
 */
@RestController
@RequestMapping("/rest/view/login")
public class LoginController {
    @Resource
    private UserService userService;

    /**
     * 用户名/密码登录接口
     *
     * @param vo 参数
     * @return 返回
     */
    @PostMapping("/sign-in.json")
    public TokenVO singIn(UserVO vo) {
        // 参数检查
        String name = vo.getName();
        ParamCheckUtil.notBlank(name, "用户名为空");
        String password = vo.getPassword();
        ParamCheckUtil.notBlank(name, "密码为空");

        TokenDTO tokenDTO = userService.signIn(name, password);

        return CloneUtil.to(tokenDTO, TokenVO.class);
    }
}
